package com.chat.service;

import com.chat.common.Message;
import com.chat.common.MessageType;
import com.chat.common.User;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class CheckEmail {
    private static final User user=new User();
    private static final Message msg=new Message();
    static boolean b;
       public static boolean checkemail(String email) {
           user.setMailbox(email);
           msg.setData(user);

           try {
               Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);
               ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
               msg.setMesType(MessageType.MESSAGE_CHECK_EMAIL);
               oos.writeObject(msg);

               ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
               Message ms = (Message) ois.readObject();

               if (ms.getMesType().equals(MessageType.MESSAGE_EMAIL_SUCCEED)) {
                      b=true;
               }
               else if(ms.getMesType().equals(MessageType.MESSAGE_EMAIL_FAIL)){
                     b=false;
               }
           } catch (IOException e) {
               e.printStackTrace();
           } catch (ClassNotFoundException e) {
               e.printStackTrace();
           }
                return b;
       }
       }
